package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.GoodsCategory;
import cn.wolfcode.car.business.mapper.GoodsCategoryMapper;
import cn.wolfcode.car.business.query.GoodsCategoryQuery;
import cn.wolfcode.car.business.service.IGoodsCategoryService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.json.ZTreeJson;
import cn.wolfcode.car.common.util.Convert;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service("GoodsCategory")
@Transactional
public class GoodsCategoryServiceImpl implements IGoodsCategoryService {

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;


    @Override
    public TablePageInfo<GoodsCategory> query(GoodsCategoryQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<GoodsCategory>(goodsCategoryMapper.selectForList(qo));
    }


    @Override
    public void save(GoodsCategory goodsCategory) {
        if (goodsCategory.getParentId() == null){
            goodsCategory.setBusiPath("1");
        }else {
            goodsCategory.setBusiPath("2");
        }
        goodsCategoryMapper.insert(goodsCategory);
    }

    @Override
    public GoodsCategory get(Long id) {
        return goodsCategoryMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(GoodsCategory goodsCategory) {
        if (goodsCategory.getParentId() == null){
            goodsCategory.setBusiPath("1");
        }else {
            goodsCategory.setBusiPath("2");
        }
        goodsCategoryMapper.updateByPrimaryKey(goodsCategory);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            GoodsCategory goodsCategory = goodsCategoryMapper.selectByPrimaryKey(dictId);
            //如果有子分类,1为父分类，2为子分类
            if ("1".equals(goodsCategory.getBusiPath())) {
                //查出所有的子分类
                List<GoodsCategory> sons = goodsCategoryMapper.selectParentId(goodsCategory.getId());
                //遍历
                for (GoodsCategory son : sons) {
                    //用id查询出库存数量
                    List<Integer> counts = goodsCategoryMapper.selectGoodsByCategoryId(son.getId());
                    //如果数量不为空
                    if (counts != null && counts.size()!=0) {
                        throw new BusinessException("该分类下还有商品，不能删除！");
                    }
                    goodsCategoryMapper.deleteByPrimaryKey(son.getId());
                }
                goodsCategoryMapper.deleteByPrimaryKey(dictId);
            }else {
                //用id查询出库存数量
                List<Integer> counts = goodsCategoryMapper.selectGoodsByCategoryId(dictId);
                //如果数量不为空
                if (counts != null && counts.size()!=0) {
                    throw new BusinessException("该分类下还有商品，不能删除！");
                }
                goodsCategoryMapper.deleteByPrimaryKey(dictId);
            }

        }

    }

    @Override
    public List<GoodsCategory> list() {
        return goodsCategoryMapper.selectAll();
    }

    @Override
    public List<GoodsCategory> selectParent() {
        return goodsCategoryMapper.selectParent();
    }

    public List<ZTreeJson> selectZTree(){
/*        List<GoodsCategory> goodsCategories = goodsCategoryMapper.selectParent();
        ArrayList<ZTreeJson> zTreeJsons = new ArrayList<>();
        for (GoodsCategory goodsCategory : goodsCategories) {
            ZTreeJson zTreeJson = new ZTreeJson();
            zTreeJson.setName(goodsCategory.getCategoryName());
            zTreeJsons.add(zTreeJson);
        }
        return zTreeJsons;*/

        List<GoodsCategory> goodsCategories = goodsCategoryMapper.selectAll();
        ArrayList<ZTreeJson> zTreeJsons = new ArrayList<>();
        for (GoodsCategory goodsCategory : goodsCategories) {
            ZTreeJson zTreeJson = new ZTreeJson();
            zTreeJson.setId(goodsCategory.getId());
            if (goodsCategory.getParentId()==null) {
                zTreeJson.setPId(0L);
            }else{
                zTreeJson.setPId(goodsCategory.getParentId());
            }
            zTreeJson.setName(goodsCategory.getCategoryName());
            zTreeJsons.add(zTreeJson);

        }

        return zTreeJsons;
    }


}
